package com.h.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author: hlj
 * @date: 2022年04月28日 17:16
 */
public class RabbitMQConnectionUtil {
    //链接地址
    public static final String RABBITMQ_HOST = "212.129.155.67";
    //端口号
    public static final int RABBITMQ_PORT = 5672;
    //用户名
    public static final String RABBITMQ_USERNAME = "guest";
    //密码
    public static final String RABBITMQ_PASSWORD = "guest";
    //
    public static final String RABBITMQ_VIRTUAL_HOST = "/";

    /**
     * 构建RabbitMQ链接对象
     *
     * @return
     */
    public static Connection getConnection() throws Exception {
        //1. 创建Connection工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2. 设置RabbitMQ的连接信息
        factory.setHost(RABBITMQ_HOST);
        factory.setPort(RABBITMQ_PORT);
        factory.setUsername(RABBITMQ_USERNAME);
        factory.setPassword(RABBITMQ_PASSWORD);
        factory.setVirtualHost(RABBITMQ_VIRTUAL_HOST);
        //3. 返回连接对象
        Connection connection = factory.newConnection();
        return connection;
    }

}
